# Appendix N
# Santiago Lopez-Cariboni, 2022. "Political Regimes and Informal Social Insurance", Comparative Political Studies.

# ---- wd ----
wd <- 'replication_materials' 
setwd(wd)

# ---- packages ----
pkgs <- c("data.table", "foreign", 'panelView', 'fect', "lubridate", 
    "ggplot2", "dplyr", "mFilter", "texreg", "AER", "xts", "interflex", "gridExtra")
suppressPackageStartupMessages(sapply(pkgs,require,character.only=T))
# Plot the effects (use Johannes Karreth's function: https://github.com/jkarreth/JKmisc/blob/master/ggintfun.R)
# devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/ggintfun.R")
source('code/functions/ggintfun.R')
# Function for time Lags
source('code/functions/lagpanel.R')


# ---- data ----
dt <- fread('data/dt_replication.csv')
dt <- dt[LDC==1, ]


# ---- Table 12 ----
dt <- dt[order(dt$iso3c, dt$year),] # sort for lagpanel
dt[finittrm == -999, finittrm:= NA]
dt[yrcurnt == -999, yrcurnt:= NA]
dt[, yrcurnt_less2:= as.numeric(yrcurnt<=2)]

m.finittrm <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * finittrm
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.finittrm,  diagnostics = TRUE)
nc.m.finittrm <- length(unique(m.finittrm$model[['as.factor(iso3c)']]))


m.lnyrcurnt <- lm(outgap.tdl ~ l.outgap.tdl 
    + l.outgap.gdp.hamilton * yrcurnt_less2
    + l.capacity
    + l.imports
    + l.exports
    + log(l.pop)
    + log(l.gdp.pcap)
    + l.elec.cons.pc
    + l.pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==1,], na.action=na.omit)
summary(m.lnyrcurnt,  diagnostics = TRUE)
nc.m.lnyrcurnt <- length(unique(m.lnyrcurnt$model[['as.factor(iso3c)']]))



v.names <- c(
    "Losses Cycle$_{t-1}$",
    "GDP Output Gap$_{t-1}$",
    "Fixed Term",
    "State Capacity$_{t-1}$",
    "Imports$_{t-1}$",
    "Exports$_{t-1}$",
    "Population (log)$_{t-1}$",
    "Real GDP per capita (log)$_{t-1}$",
    "Electricity Consumption$_{t-1}$",
    "Population Density$_{t-1}$",
    "Fixed-Term $\\times$ GDP Output Gap$_{t-1}$",
    "Years left in Term$<=2$",
    "Years left in Term$<=2$ $\\times$ GDP Output Gap$_{t-1}$"
    )



texreg(list(m.finittrm, m.lnyrcurnt),
    file= "tables/OA/T12_elections.tex",
    label="tab:elections",
    custom.model.names = c('(1)','(2)'),
    caption="Cyclicality of Electricity Losses in Democracies Moderated by Time Left in Offfice and Fixed Term Incumbency",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes"), 
        "Country fixed-effects" = c("Yes", "Yes"),
        "Number of countries"= c(nc.m.finittrm, nc.m.lnyrcurnt))
    ,
    # reorder.gof = c(1, 2, 5, 3, 4),
    custom.coef.names=v.names
    )





# ---- Figure N1 ----
pdf("figures/OA/N1_finittrm.pdf", width = 8, height = 6)
ggintfun(obj = m.finittrm, varnames = c("l.outgap.gdp.hamilton", "finittrm"), 
         varlabs = c("GDP Output Gap", "Finite Term in Office"),
         title = FALSE, rug = TRUE,
         twoways = TRUE)
dev.off()

pdf("figures/OA/N1_lnyrcurnt.pdf", width = 8, height = 6)
ggintfun(obj = m.lnyrcurnt, varnames = c("l.outgap.gdp.hamilton", "yrcurnt_less2"), 
         varlabs = c("GDP Output Gap", "Years left in Term<=2"),
         title = FALSE, rug = TRUE,
         twoways = TRUE)
dev.off()

